﻿#include "stackinfomanager.h"
#include "../../Algorithm/AlgStackCommon/StackSetting/stackshapmanager.h"
#include "../component_log.h"

#define PLATE_W (1000)
#define PLATE_L (1200)
#define PLATE_H (1500)
StackInfoManager::StackInfoManager(QObject *parent)
	: ComponentBase(parent)
{
	m_name = u8"垛形数据管理器";
	m_errmsg = "";
	m_type = Component::component_stackInfo;
}

StackInfoManager::~StackInfoManager()
{
}
// 组件初始化处理函数
int StackInfoManager::ComponentInit()
{
	m_manger = new StackShapManager();
	if (m_manger->Init() <= 0) {
		log_error("stack shap manager init failed");
		return -99;
	}

	return 0;
}
/*自检接口*/
bool StackInfoManager::ComponentCheckSelfState()
{
	return true;
}
/*复位接口*/
bool StackInfoManager::ComponentSelfReset()
{
	return true;
}

json_struct(StackInfoAns,
	mapping(int, num);
prop(&num);
	)


// 命令执行接口
bool StackInfoManager::ExcuteCmd(int cmd, const QByteArray& input, QByteArray& output)
{
	if (cmd == 1) {
		//Size sku = {};
		SizeJ sku1 = {};
		sku1.fromJson(input);
		Size sku = {};
		sku.TransFromSizeJ(sku1);
		Size plate = {};
		plate.width = PLATE_W;
		plate.height = PLATE_H;
		plate.length = PLATE_L;
		auto info = m_manger->ChooseStackForSku(sku, plate);
		StackInfoAns ans = {};
		if (info.id == 0) {
			ans.num = -1;
			return false;
		} else {
			ans.num = info.maxNum;
		}
		output = ans.toByteArray();
		return true;
	}


	return false;
}
